<template>
  <div class="mall-templates-page">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">商城装修模板</span>
          <el-input
            v-model="searchText"
            placeholder="搜索模板..."
            style="width: 300px"
            clearable
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>
      </template>

      <!-- 分类 -->
      <div class="category-tabs">
        <el-tag
          v-for="cat in categories"
          :key="cat.value"
          :type="selectedCategory === cat.value ? 'primary' : 'info'"
          class="category-tag"
          @click="selectedCategory = cat.value"
        >
          {{ cat.label }}
        </el-tag>
      </div>

      <!-- 模板列表 -->
      <el-row :gutter="20" style="margin-top: 20px">
        <el-col :span="6" v-for="template in filteredTemplates" :key="template.id">
          <el-card class="template-card" shadow="hover">
            <div class="template-preview">
              <img :src="template.cover" :alt="template.name" />
              <div class="template-overlay">
                <el-button type="primary" @click="handleUse(template)">
                  使用模板
                </el-button>
              </div>
            </div>
            <div class="template-info">
              <h3>{{ template.name }}</h3>
              <p>{{ template.desc }}</p>
              <div class="template-meta">
                <el-tag size="small">{{ template.category }}</el-tag>
                <span class="template-stats">
                  <el-icon><View /></el-icon>
                  {{ template.views }}
                </span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { Search, View } from '@element-plus/icons-vue'

const router = useRouter()
const searchText = ref('')
const selectedCategory = ref('all')

const categories = [
  { label: '全部', value: 'all' },
  { label: '首页模板', value: 'home' },
  { label: '分类页', value: 'category' },
  { label: '活动页', value: 'activity' }
]

const templates = ref([
  {
    id: 1,
    name: '时尚首页',
    desc: '适合时尚服装类商城',
    category: '首页模板',
    cover: 'https://via.placeholder.com/300x500/409eff/ffffff?text=Fashion+Home',
    views: 1234
  },
  {
    id: 2,
    name: '美妆首页',
    desc: '化妆品美妆类商城',
    category: '首页模板',
    cover: 'https://via.placeholder.com/300x500/f56c6c/ffffff?text=Beauty+Home',
    views: 987
  },
  {
    id: 3,
    name: '电子产品首页',
    desc: '3C数码电子产品',
    category: '首页模板',
    cover: 'https://via.placeholder.com/300x500/67c23a/ffffff?text=Tech+Home',
    views: 765
  },
  {
    id: 4,
    name: '双11活动页',
    desc: '大促活动专用',
    category: '活动页',
    cover: 'https://via.placeholder.com/300x500/e6a23c/ffffff?text=11.11+Sale',
    views: 2345
  }
])

const filteredTemplates = computed(() => {
  let result = templates.value
  
  if (selectedCategory.value !== 'all') {
    result = result.filter(t => t.category === selectedCategory.value)
  }
  
  if (searchText.value) {
    result = result.filter(t => 
      t.name.includes(searchText.value) || t.desc.includes(searchText.value)
    )
  }
  
  return result
})

const handleUse = (template) => {
  ElMessage.success(`正在使用模板: ${template.name}`)
  setTimeout(() => {
    router.push('/mall/diy-designer')
  }, 500)
}
</script>

<style scoped>
.mall-templates-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.category-tabs {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.category-tag {
  cursor: pointer;
  padding: 8px 16px;
}

.template-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.template-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}

.template-preview {
  position: relative;
  overflow: hidden;
  border-radius: 4px;
  margin: -20px -20px 15px -20px;
}

.template-preview img {
  width: 100%;
  height: 300px;
  object-fit: cover;
}

.template-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s;
}

.template-card:hover .template-overlay {
  opacity: 1;
}

.template-info h3 {
  margin: 0 0 8px 0;
  font-size: 16px;
}

.template-info p {
  margin: 0 0 10px 0;
  font-size: 13px;
  color: #606266;
}

.template-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.template-stats {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #909399;
  font-size: 13px;
}
</style>

